package com.cn.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.pojo.Adv;
import com.cn.response.Result;
import com.cn.service.AdvService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 功能描述: <br>
 *
 * @program: Wx-mini-program
 * @description: 轮播图
 * @author: Mr.TYJ
 * @create: 2022-05-23 18:13
 **/
@RestController
@CrossOrigin
@Slf4j
@Api(tags = "用户管理de")
@RequestMapping("/adv")
public class AdvController {

    @Autowired
   private AdvService advService;

    @ApiOperation(value = "分页查询轮播图")
    @GetMapping("/selectGetAll")
    public Result selectGetAll(@RequestParam(value = "current",required = true,defaultValue ="1") Integer current,
                                  @RequestParam(value = "size",required = true,defaultValue ="3")Integer size,
                                  @RequestParam(value = "advTitle",required = false) String advTitle,
                                  @RequestParam(value = "statusId",required = false)Integer statusId){
        Page<Adv> page=new Page<>(current,size);
        Page<Adv> pageList=advService.selectAll(page,advTitle,statusId);
        return Result.ok().data("pageList",pageList);
    }
    @ApiOperation(value = "根据id删除轮播图信息")
    @PostMapping("/getDeleteById")
    public Result getDeleteById(@RequestParam(value = "id",required = true) Integer id){
        Integer num=advService.getDeleteById(id);
        if (num>0){
        return Result.ok().data("code",200);
        }else {
            return Result.ok().data("code",0);
        }
    }
    @ApiOperation(value = "根据id修改轮播图参数")
    @PostMapping("/getUpdateById")
    public Result getUpdateById(@RequestBody Adv adv){
        boolean num =advService.updateById(adv);
        if (num){
            return Result.ok().data("code",200);
        }else {
            return Result.ok().data("code",0);
        }
    }
    @ApiOperation(value = "根据id查询轮播图参数")
    @PostMapping("/selectAllById")
    public Result selectAllById(@RequestParam(value = "id",required = true) Integer id){
        Adv adv=advService.selectAllById(id);
        return Result.ok().data("adv",adv);
    }
    @ApiOperation(value = "添加轮播图")
    @PostMapping("/getInsert")
    public Result getInsert(@RequestBody Adv adv){
        Integer num=advService.getInsert(adv);
        if (num>0){
            return Result.ok().data("code",200);
        }else {
            return Result.ok().data("code",0);
        }
    }
    @ApiModelProperty(value = "修改状态", notes = "修改状态")
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestParam(value = "id",required = true) Integer id,
                               @RequestParam(value = "statusId",required = true)Byte statusId) {
        Adv user = new Adv();
        user.setStatusId(statusId);
        UpdateWrapper<Adv> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        boolean update = advService.update(user, updateWrapper);
        if (update) {
            return Result.ok().message("修改状态成功");
        }
        return Result.error().message("修改状态失败真晦气");
    }
}
